import request from "@/utils/request";
// 菜单基础URL
const MENU_BASE_URL = "/api/v1/menus";

// let data = [
//   {
//     path: "/system",
//     component: "Layout",
//     redirect: "/system/user",
//     name: "/system",
//     meta: {
//       title: "系统管理",
//       icon: "system",
//       hidden: false,
//       alwaysShow: false,
//       params: null,
//     },
//     children: [
//       {
//         path: "user",
//         component: "system/user/index",
//         name: "User",
//         meta: {
//           title: "用户管理",
//           icon: "el-icon-User",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "role",
//         component: "system/role/index",
//         name: "Role",
//         meta: {
//           title: "角色管理",
//           icon: "role",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "menu",
//         component: "system/menu/index",
//         name: "SysMenu",
//         meta: {
//           title: "菜单管理",
//           icon: "menu",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "dept",
//         component: "system/dept/index",
//         name: "Dept",
//         meta: {
//           title: "部门管理",
//           icon: "tree",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "dict",
//         component: "system/dict/index",
//         name: "Dict",
//         meta: {
//           title: "字典管理",
//           icon: "dict",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "dict-data",
//         component: "system/dict/data",
//         name: "DictItem",
//         meta: {
//           title: "字典数据",
//           icon: "",
//           hidden: true,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "question",
//         component: "system/question/index",
//         name: "Question",
//         meta: {
//           title: "题目管理",
//           icon: "el-icon-document",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//         children: [
//           {
//             path: "edit",
//             component: "system/question/edit",
//             name: "QuestionEdit",
//             meta: {
//               title: "题目编辑",
//               icon: "",
//               hidden: true,
//               keepAlive: false,
//               alwaysShow: false,
//               params: null,
//             },
//           },
//         ],
//       },
//     ],
//   },
//   {
//     path: "/multi-level",
//     component: "Layout",
//     name: "/multiLevel",
//     meta: {
//       title: "多级菜单",
//       icon: "cascader",
//       hidden: false,
//       alwaysShow: true,
//       params: null,
//     },
//     children: [
//       {
//         path: "multi-level1",
//         component: "demo/multi-level/level1",
//         name: "MultiLevel1",
//         meta: {
//           title: "菜单一级",
//           icon: "",
//           hidden: false,
//           alwaysShow: true,
//           params: null,
//         },
//         children: [
//           {
//             path: "multi-level2",
//             component: "demo/multi-level/children/level2",
//             name: "MultiLevel2",
//             meta: {
//               title: "菜单二级",
//               icon: "",
//               hidden: false,
//               alwaysShow: false,
//               params: null,
//             },
//             children: [
//               {
//                 path: "multi-level3-1",
//                 component: "demo/multi-level/children/children/level3-1",
//                 name: "MultiLevel31",
//                 meta: {
//                   title: "菜单三级-1",
//                   icon: "",
//                   hidden: false,
//                   keepAlive: true,
//                   alwaysShow: false,
//                   params: null,
//                 },
//               },
//               {
//                 path: "multi-level3-2",
//                 component: "demo/multi-level/children/children/level3-2",
//                 name: "MultiLevel32",
//                 meta: {
//                   title: "菜单三级-2",
//                   icon: "",
//                   hidden: false,
//                   keepAlive: true,
//                   alwaysShow: false,
//                   params: null,
//                 },
//               },
//             ],
//           },
//         ],
//       },
//     ],
//   },
//   {
//     path: "/api",
//     component: "Layout",
//     name: "/api",
//     meta: {
//       title: "接口文档",
//       icon: "api",
//       hidden: false,
//       alwaysShow: true,
//       params: null,
//     },
//     children: [
//       {
//         path: "apifox",
//         component: "demo/api/apifox",
//         name: "Apifox",
//         meta: {
//           title: "Apifox",
//           icon: "api",
//           hidden: false,
//           keepAlive: true,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//     ],
//   },
//   {
//     path: "/doc",
//     component: "Layout",
//     redirect: "https://juejin.cn/post/7228990409909108793",
//     name: "/doc",
//     meta: {
//       title: "平台文档",
//       icon: "document",
//       hidden: false,
//       alwaysShow: false,
//       params: null,
//     },
//     children: [
//       {
//         path: "internal-doc",
//         component: "demo/internal-doc",
//         name: "InternalDoc",
//         meta: {
//           title: "平台文档(内嵌)",
//           icon: "document",
//           hidden: false,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//       {
//         path: "https://juejin.cn/post/7228990409909108793",
//         name: "Https://juejin.cn/post/7228990409909108793",
//         meta: {
//           title: "平台文档(外链)",
//           icon: "el-icon-link",
//           hidden: false,
//           alwaysShow: false,
//           params: null,
//         },
//       },
//     ],
//   },
// ]
const data = [
  {
    path: "/user",
    component: "Layout",
    redirect: "/user/student",
    name: "UserManage",
    meta: {
      title: "用户管理",
      icon: "system",
      hidden: false,
      alwaysShow: false,
      params: null,
    },
    children: [
      {
        path: "student",
        component: "user/StudentView",
        name: "UserStudentList",
        meta: {
          title: "学生列表",
          icon: "el-icon-User",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "administrator",
        component: "user/AdminView",
        name: "UserAdminList",
        meta: {
          title: "管理员列表",
          icon: "role",
          hidden: false,
          keepAlive: true,
          alwaysShow: false,
          params: null,
        },
      },
      {
        path: "student-add",
        component: "user/StudentEditView",
        name: "StudentAddView",
        meta: { title: "学生新增", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "student-edit",
        component: "user/StudentEditView",
        name: "StudentEditView",
        meta: { title: "学生编辑", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "admin-add",
        component: "user/AdminEditView",
        name: "AdminAddView",
        meta: { title: "管理员新增", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "admin-edit",
        component: "user/AdminEditView",
        name: "AdminEditView",
        meta: { title: "管理员编辑", hidden: true, keepAlive: false, params: null },
      },
    ],
  },
  {
    path: "/paper",
    component: "Layout",
    redirect: "/paper/papers",
    name: "PaperManage",
    meta: { title: "卷题管理", icon: "system", hidden: false, alwaysShow: false, params: null },
    children: [
      {
        path: "papers",
        component: "user/PaperView",
        name: "PaperList",
        meta: {
          title: "试卷列表",
          icon: "el-icon-document",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
      {
        path: "paper-add",
        component: "user/PaperEditView",
        name: "PaperAddView",
        meta: { title: "试卷新增", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "paper-edit",
        component: "user/PaperEditView",
        name: "PaperEditView",
        meta: { title: "试卷编辑", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "questions",
        component: "user/QuestionView",
        name: "QuestionList",
        meta: {
          title: "题目列表",
          icon: "el-icon-edit",
          hidden: false,
          keepAlive: true,
          params: null,
        },
        children: [
          {
            path: "edit",
            component: "user/QuestionEditView",
            name: "QuestionEdit",
            meta: { title: "题目编辑", hidden: true, keepAlive: false, params: null },
          },
        ],
      },
    ],
  },
  {
    path: "/task",
    component: "Layout",
    redirect: "/task/tasks",
    name: "TaskManage",
    meta: { title: "任务管理", icon: "system", hidden: false, alwaysShow: false, params: null },
    children: [
      {
        path: "tasks",
        component: "user/TaskView",
        name: "TaskList",
        meta: {
          title: "任务列表",
          icon: "el-icon-tickets",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
      {
        path: "create",
        component: "user/TaskCreateView",
        name: "TaskCreate",
        meta: {
          title: "任务创建",
          icon: "el-icon-plus",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
    ],
  },
  {
    path: "/education",
    component: "Layout",
    redirect: "/education/subject",
    name: "EducationManage",
    meta: { title: "教育管理", icon: "system", hidden: false, alwaysShow: true, params: null },
    children: [
      {
        path: "subject",
        component: "user/SubjectView",
        name: "SubjectList",
        meta: { title: "学科列表", icon: "system", hidden: false, keepAlive: true, params: null },
      },
      {
        path: "subject/edit",
        component: "user/SubjectEditView",
        name: "SubjectEdit",
        meta: { title: "学科编辑", icon: "system", hidden: true, keepAlive: false, params: null },
      },
      {
        path: "subject/edit",
        component: "user/SubjectEditView",
        name: "SubjectEdit",
        meta: { title: "学科添加", icon: "system", hidden: true, keepAlive: false, params: null },
      },
    ],
  },
  {
    path: "/grade",
    component: "Layout",
    redirect: "/grade/answers",
    name: "GradeManage",
    meta: { title: "成绩管理", icon: "system", hidden: false, alwaysShow: true, params: null },
    children: [
      {
        path: "answers",
        component: "user/AnswerView",
        name: "AnswerList",
        meta: {
          title: "答卷列表",
          icon: "el-icon-document-checked",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
    ],
  },
  {
    path: "/message",
    component: "Layout",
    redirect: "/message/list",
    name: "MessageCenter",
    meta: { title: "消息中心", icon: "system", hidden: false, alwaysShow: false, params: null },
    children: [
      {
        path: "list",
        component: "user/MessageView",
        name: "MessageList",
        meta: {
          title: "消息列表",
          icon: "el-icon-message",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
      {
        path: "send",
        component: "user/MessageSendView",
        name: "MessageSend",
        meta: {
          title: "消息发送",
          icon: "el-icon-upload",
          hidden: false,
          keepAlive: true,
          params: null,
        },
      },
    ],
  },
  {
    path: "/log",
    component: "Layout",
    redirect: "/log/user-log",
    name: "LogCenter",
    meta: { title: "日志中心", icon: "system", hidden: false, alwaysShow: true, params: null },
    children: [
      {
        path: "user-log",
        component: "user/UserLogView",
        name: "UserLogView",
        meta: { title: "用户日志", hidden: false, keepAlive: false, params: null },
      },
    ],
  },
];

const MenuAPI = {
  /**
   * 获取当前用户的路由列表
   * <p/>
   * 无需传入角色，后端解析token获取角色自行判断是否拥有路由的权限
   *
   * @returns 路由列表
   */
  getRoutes() {
    return Promise.resolve(data);
  },

  /**
   * 获取菜单树形列表
   *
   * @param queryParams 查询参数
   * @returns 菜单树形列表
   */
  getList(queryParams: MenuQuery) {
    return request<any, MenuVO[]>({
      url: `${MENU_BASE_URL}`,
      method: "get",
      params: queryParams,
    });
  },

  /**
   * 获取菜单下拉数据源
   *
   * @returns 菜单下拉数据源
   */
  getOptions(onlyParent?: boolean) {
    return request<any, OptionType[]>({
      url: `${MENU_BASE_URL}/options`,
      method: "get",
      params: { onlyParent: onlyParent },
    });
  },

  /**
   * 获取菜单表单数据
   *
   * @param id 菜单ID
   */
  getFormData(id: string) {
    return request<any, MenuForm>({
      url: `${MENU_BASE_URL}/${id}/form`,
      method: "get",
    });
  },

  /**
   * 添加菜单
   *
   * @param data 菜单表单数据
   * @returns 请求结果
   */
  create(data: MenuForm) {
    return request({
      url: `${MENU_BASE_URL}`,
      method: "post",
      data: data,
    });
  },

  /**
   * 修改菜单
   *
   * @param id 菜单ID
   * @param data 菜单表单数据
   * @returns 请求结果
   */
  update(id: string, data: MenuForm) {
    return request({
      url: `${MENU_BASE_URL}/${id}`,
      method: "put",
      data: data,
    });
  },

  /**
   * 删除菜单
   *
   * @param id 菜单ID
   * @returns 请求结果
   */
  deleteById(id: string) {
    return request({
      url: `${MENU_BASE_URL}/${id}`,
      method: "delete",
    });
  },
};

export default MenuAPI;

/** 菜单查询参数 */
export interface MenuQuery {
  /** 搜索关键字 */
  keywords?: string;
}

/** 菜单视图对象 */
export interface MenuVO {
  /** 子菜单 */
  children?: MenuVO[];
  /** 组件路径 */
  component?: string;
  /** ICON */
  icon?: string;
  /** 菜单ID */
  id?: string;
  /** 菜单名称 */
  name?: string;
  /** 父菜单ID */
  parentId?: string;
  /** 按钮权限标识 */
  perm?: string;
  /** 跳转路径 */
  redirect?: string;
  /** 路由名称 */
  routeName?: string;
  /** 路由相对路径 */
  routePath?: string;
  /** 菜单排序(数字越小排名越靠前) */
  sort?: number;
  /** 菜单 */
  type?: number;
  /** 菜单是否可见(1:显示;0:隐藏) */
  visible?: number;
}

/** 菜单表单对象 */
export interface MenuForm {
  /** 菜单ID */
  id?: string;
  /** 父菜单ID */
  parentId?: string;
  /** 菜单名称 */
  name?: string;
  /** 菜单是否可见(1-是 0-否) */
  visible: number;
  /** ICON */
  icon?: string;
  /** 排序 */
  sort?: number;
  /** 路由名称 */
  routeName?: string;
  /** 路由路径 */
  routePath?: string;
  /** 组件路径 */
  component?: string;
  /** 跳转路由路径 */
  redirect?: string;
  /** 菜单 */
  type?: number;
  /** 权限标识 */
  perm?: string;
  /** 【菜单】是否开启页面缓存 */
  keepAlive?: number;
  /** 【目录】只有一个子路由是否始终显示 */
  alwaysShow?: number;
  /** 参数 */
  params?: KeyValue[];
}

interface KeyValue {
  key: string;
  value: string;
}

/** RouteVO，路由对象 */
export interface RouteVO {
  /** 子路由列表 */
  children?: RouteVO[];
  /** 组件路径 */
  component?: string;
  /** 路由属性 */
  meta?: Meta;
  /** 路由名称 */
  name?: string;
  /** 路由路径 */
  path?: string;
  /** 跳转链接 */
  redirect?: string;
}

/** Meta，路由属性 */
export interface Meta {
  /** 【目录】只有一个子路由是否始终显示 */
  alwaysShow?: boolean;
  /** 是否隐藏(true-是 false-否) */
  hidden?: boolean;
  /** ICON */
  icon?: string;
  /** 【菜单】是否开启页面缓存 */
  keepAlive?: boolean;
  /** 路由title */
  title?: string;
}
